﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace Red.Controls
{
    public class HelpAdorner : ContentControl
    {
        #region Dependency Properties

        public static readonly DependencyProperty HelpProperty = DependencyProperty.Register( "Help", typeof( string ), typeof( HelpAdorner ) );
        public static readonly DependencyProperty PlacementProperty = DependencyProperty.Register( "Placement", typeof( PlacementMode ), typeof( HelpAdorner ), new PropertyMetadata( PlacementMode.Right ) );

        public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register( "IsOpen", typeof( bool ), typeof( HelpAdorner ) );
        public static readonly DependencyProperty IsMouseOverFlagProperty = DependencyProperty.Register( "IsMouseOverFlag", typeof( bool ), typeof( HelpAdorner ), new PropertyMetadata( FlagChanged ) );
        public static readonly DependencyProperty IsKeyboardFocusedFlagProperty = DependencyProperty.Register( "IsKeyboardFocusedFlag", typeof( bool ), typeof( HelpAdorner ), new PropertyMetadata( FlagChanged ) );
        public static readonly DependencyProperty IgnoreKeyboardFocusFlagProperty = DependencyProperty.Register( "IgnoreKeyboardFocusFlag", typeof( bool ), typeof( HelpAdorner ), new PropertyMetadata( true, FlagChanged ) );
        public static readonly DependencyProperty IsUserInputIgnoredProperty = DependencyProperty.Register( "IsUserInputIgnored", typeof( bool ), typeof( HelpAdorner ), new PropertyMetadata( FlagChanged ) );

        #endregion Dependency Properties

        #region Properties

        public string Help
        {
            get { return (string)GetValue( HelpProperty ); }
            set { SetValue( HelpProperty, value ); }
        }

        public PlacementMode Placement
        {
            get { return (PlacementMode)GetValue( PlacementProperty ); }
            set { SetValue( PlacementProperty, value ); }
        }

        public bool IsOpen
        {
            get { return (bool)GetValue( IsOpenProperty ); }
            set { SetValue( IsOpenProperty, value ); }
        }
        
        public bool IsMouseOverFlag
        {
            get { return (bool)GetValue( IsMouseOverFlagProperty ); }
            set { SetValue( IsMouseOverFlagProperty, value ); }
        }

        public bool IsKeyboardFocusedFlag
        {
            get { return (bool)GetValue( IsKeyboardFocusedFlagProperty ); }
            set { SetValue( IsKeyboardFocusedFlagProperty, value ); }
        }

        public bool IgnoreKeyboardFocusFlag
        {
            get { return (bool)GetValue( IgnoreKeyboardFocusFlagProperty ); }
            set { SetValue( IgnoreKeyboardFocusFlagProperty, value ); }
        }

        public bool IsUserInputIgnored
        {
            get { return (bool)GetValue( IsUserInputIgnoredProperty ); }
            set { SetValue( IsUserInputIgnoredProperty, value ); }
        }

        #endregion Properties

        #region Event Handlers

        private static void FlagChanged( DependencyObject d, DependencyPropertyChangedEventArgs e )
        {
            HelpAdorner adorner = (HelpAdorner)d;

            if( !adorner.IsUserInputIgnored )
            {
                adorner.IsOpen = ( !adorner.IgnoreKeyboardFocusFlag && adorner.IsKeyboardFocusedFlag ) || adorner.IsMouseOverFlag;
            }
        }

        #endregion Event Handlers
    }
}
